package com.mjh.zhujianju.service.visit.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.visit.VisitUserMapper;
import com.mjh.zhujianju.enums.SexEnum;
import com.mjh.zhujianju.model.visit.VisitUserEO;
import com.mjh.zhujianju.service.visit.VisitUserService;
import com.mjh.zhujianju.vo.visit.VisitUserVO;

@Service
public class VisitUserServiceImpl extends BaseServiceImpl<VisitUserEO> implements VisitUserService{
	@Resource
	private VisitUserMapper visitUserMapper;
	
	@Resource
	private SysUserService userService;
	
	public VisitUserServiceImpl(VisitUserMapper visitUserMapper) {
		super(visitUserMapper);
	}

	@Override
	@Transactional
	public void saveInfo(VisitUserEO eo) throws XTBusinessException {
		eo.setStatus(EStatus.active.getCode());
		// 计算总进度
		this.saveSelective(eo);
	}

	@Override
	@Transactional
	public void updateInfo(VisitUserEO eo) throws XTBusinessException {
		eo.setStatus(EStatus.active.getCode());
		this.updateByPrimaryKeySelective(eo);
	}

	@Override
	public PagedResult<VisitUserVO> getVOPage(VisitUserVO vo) {
		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		List<VisitUserEO> list = visitUserMapper.selectPagination(vo);
		PagedResult<VisitUserVO> result = new PagedResult<>();
		if(!XTListUtils.isEmpty(list)) {
			List<Long> userIds = new ArrayList<>();
			for (VisitUserEO v : list) {
				userIds.add(v.getUserId());
			}
			PagedResult<VisitUserEO> page = PagedBeanUtil.toPagedResult(list);
			Map<Long, SysUser> userMap =  userService.getMapByIdList(userIds);
			BeanUtils.copyProperties(page, result);
			if(!XTListUtils.isEmpty(list)) {
				List<VisitUserVO> voList= new ArrayList<>();
				for (VisitUserEO v : list) {
					VisitUserVO o = new VisitUserVO();
					BeanUtils.copyProperties(v, o);
					if(userMap!=null) {
						SysUser user = userMap.get(v.getUserId());
						if(user!=null) {
							o.setPhone(user.getPhone());
						}
					}
					o.setSexName(SexEnum.getName(o.getSex()));
					voList.add(o);
				}
				result.setDataList(voList);
			}
		}
		return result;
	}

	@Override
	@Transactional
	public int delByPrimaryKey(VisitUserEO t) throws XTBusinessException {
		if(t.getUserId()==null) {
			throw new XTBusinessException("用户信息错误");
		}
		SysUser user = new  SysUser();
		user.setId(t.getUserId());
		userService.delByPrimaryKey(user);
		return super.delByPrimaryKey(t);
	}

	@Override
	public List<VisitUserVO> getVisitUserList(String remark) {
		List<VisitUserVO>  list = visitUserMapper.selectVisitUserListByRemark(remark);
		return list;
	}

	@Override
	public VisitUserVO getInfo(Long userId) {
		if(userId==null) {
			return null;
		}
		VisitUserVO vo = 	visitUserMapper.selectVisitUserByUserId(userId);
		if(vo!=null) {
			vo.setSexName(SexEnum.getName(vo.getSex()));
		}
		return vo;
	}

	@Override
	public List<VisitUserVO> getVOList(VisitUserVO vo) {
		List<VisitUserEO> list = visitUserMapper.selectPagination(vo);
		List<VisitUserVO> voList= new ArrayList<>();
		if(!XTListUtils.isEmpty(list)) {
			List<Long> userIds = new ArrayList<>();
			for (VisitUserEO v : list) {
				userIds.add(v.getUserId());
			}
			Map<Long, SysUser> userMap =  userService.getMapByIdList(userIds);
			if(!XTListUtils.isEmpty(list)) {
				for (VisitUserEO v : list) {
					VisitUserVO o = new VisitUserVO();
					BeanUtils.copyProperties(v, o);
					if(userMap!=null) {
						SysUser user = userMap.get(v.getUserId());
						if(user!=null) {
							o.setPhone(user.getPhone());
						}
					}
					o.setSexName(SexEnum.getName(o.getSex()));
					voList.add(o);
				}
			}
	}
		return voList;
	}
}
